package it.xiuxian.dagongyuzhou


import android.graphics.Color
import android.util.Log
import android.util.SparseArray
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.afollestad.materialdialogs.MaterialDialog
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.zzhoujay.richtext.RichText
import it.xiuxian.homepage.fragment.HomePageFragment
import it.xiuxian.lib.base.App
import it.xiuxian.dagongyuzhou.presenter.MainActivityPresenter
import it.xiuxian.lib.utils.MediaInitHelper
import it.xiuxian.lib.utils.MediaServiceImpl
import it.xiuxian.lib.base.AppManager
import it.xiuxian.lib.base.ArouterAddress
import it.xiuxian.lib.base.BaseActivity
import it.xiuxian.lib.bean.NoticeBean
import it.xiuxian.lib.bean.TabItem
import it.xiuxian.lib.http.SPConfig
import it.xiuxian.lib.utils.*
import it.xiuxian.personcenter.fragment.PersonCenterFragment
import it.xiuxian.swindle.fragment.GongYIFragment
import it.xiuxian.tradecenter.fragment.TradCenterFragment
import it.xiuxian.xingqiu.fragment.XingQiuFragment
import kotlinx.android.synthetic.main.activity_main.*

import java.util.*



@Route(path = ArouterAddress.MAINACTIVITY)
class MainActivity : BaseActivity<MainActivityPresenter>(),View.OnClickListener{
    @JvmField
    @Autowired
    var sessionId:String?=null
    var s:String?=null
    private var fragments: ArrayList<Fragment>? = null
    var tabMap = SparseArray<Fragment>()
    private var curPos = 0
    val tabItems = ArrayList<TabItem>()
    val group = ArrayList<LinearLayout>()
    override fun initView() {
        ARouter.getInstance().inject(this)
        tabItems.clear()
        group.clear()
        group.add(tab1)
        group.add(tab2)
        group.add(tab3)
        group.add(tab4)
        group.add(tab5)
        tabItems.add(
            TabItem(
                "首页",
                R.mipmap.lib_icon_menu_first_unselect,
                R.mipmap.lib_icon_menu_first_select,
                R.color.libTextColorHint,
                R.color.libSelect
            )
        )
        tabItems.add(
            TabItem(
                "照片墙",
                R.mipmap.lib_icon_menu_second_unselect,
                R.mipmap.lib_icon_menu_second_select,
                R.color.libTextColorHint,
                R.color.libSelect
            )
        )
        tabItems.add(
            TabItem(
                "公益照片",
                R.mipmap.lib_icon_menu_five_select,
                R.mipmap.lib_icon_menu_five_select,
                R.color.libTextColorHint,
                R.color.libSelect
            )
        )
        tabItems.add(
            TabItem(
                "短视频",
                R.mipmap.lib_third_unselected,
                R.mipmap.lib_third_select,
                R.color.libTextColorHint,
                R.color.libSelect
            )
        )
        tabItems.add(
            TabItem(
                "我的",
                R.mipmap.lib_icon_menu_four_unselect,
                R.mipmap.lib_icon_menu_four_select,
                R.color.libTextColorHint,
                R.color.libSelect
            )
        )
        resetResouce()
        fragments = ArrayList()
        fragments!!.add(
            (ARouter.getInstance().build(ArouterAddress.HOMEPAGEFRAGMENT).withString("sessionId",sessionId).withInt("flag",1).navigation() as HomePageFragment)
        )
        fragments!!.add(
            (ARouter.getInstance().build(ArouterAddress.XINGQIUFRAGMENT).navigation() as XingQiuFragment)
        )
        fragments!!.add(
            (ARouter.getInstance().build(ArouterAddress.GONGYIFRAGMENT).navigation() as GongYIFragment)
        )
        fragments!!.add(
            TradCenterFragment.newInstance()
        )

        fragments!!.add(
            (ARouter.getInstance().build(ArouterAddress.PERSONCENTERFRAGMENT).navigation() as PersonCenterFragment)
        )
        supportFragmentManager
            .beginTransaction()
            .add(R.id.frameLayout, fragments!![0])
            .show(fragments!![0])
            .commitAllowingStateLoss()
            tabMap.append(0,fragments!![0])
        image1.setImageDrawable(getDrawable(tabItems[0].selectedIcon))
        tv1.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
        LiveDataBus.get().with(RxBusCode.KANSHIPIN).observe(this, androidx.lifecycle.Observer {
            resetResouce()
            image4.setImageDrawable(getDrawable(tabItems[3].selectedIcon))
            tv4.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
            // frameLayout.setCurrentItem(3,false)
            changeTab(3)
        })
    }

    override fun initEvent(){
        tab1.setOnClickListener(this)
        tab2.setOnClickListener(this)
        tab3.setOnClickListener(this)
        tab4.setOnClickListener(this)
        tab5.setOnClickListener(this)
        if(!MediaInitHelper.isDPInited){
            MediaServiceImpl.Companion.instance.init(App.INSTANCE)
        }
    }

    override val layoutId: Int
        get() = R.layout.activity_main
    override val presenter: MainActivityPresenter
        get() = MainActivityPresenter()
    private fun changeTab(position: Int){
        if (tabMap[position] == null) {
            supportFragmentManager
                .beginTransaction()
                .add(R.id.frameLayout, fragments!![position])
                //.add()
                .hide(fragments!![curPos])
                .show(fragments!![position])
                .commitAllowingStateLoss()
            tabMap.put(position, fragments!![position])
        } else{
            supportFragmentManager
                .beginTransaction()
                .hide(fragments!![curPos])
                .show(fragments!![position])
                .commitAllowingStateLoss()
        }
        curPos = position
    }
    private var oldTime: Long = 0
    override fun onBackPressed(){
        val nowTime = Date().time
        if (nowTime - oldTime <= 2000) {
            AppManager.getSingleton().AppExit(this)
        } else {
            oldTime = nowTime
            //ToastUtil.showShort(App.INSTANCE.getString(R.string.libbaseActivity1))
            Toast.makeText(this, "再次点击退出程序",Toast.LENGTH_SHORT).show()
        }
    }

    override fun onClick(v: View?) {
        resetResouce()
       //ToastUtil.showShort(this.fragments!!.size.toString())
       //var d =  Thread.currentThread()
        when(v!!.id){
            R.id.tab1-> {
                image1.setImageDrawable(getDrawable(tabItems[0].selectedIcon))
                tv1.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
                //frameLayout.setCurrentItem(0,false)
                changeTab(0)
            }
            R.id.tab2-> {
                image2.setImageDrawable(getDrawable(tabItems[1].selectedIcon))
                tv2.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
                //frameLayout.setCurrentItem(1,false)
                changeTab(1)
            }
            R.id.tab3-> {
                image3.setImageDrawable(getDrawable(tabItems[2].selectedIcon))
                tv3.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
               // frameLayout.setCurrentItem(2,false)
                changeTab(2)
            }
            R.id.tab4-> {
                image4.setImageDrawable(getDrawable(tabItems[3].selectedIcon))
                tv4.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
               // frameLayout.setCurrentItem(3,false)
                changeTab(3)
            }
            R.id.tab5-> {
                image5.setImageDrawable(getDrawable(tabItems[4].selectedIcon))
                tv5.setTextColor(resources.getColor(tabItems[0].getTitleSelectColor()))
               // frameLayout.setCurrentItem(4,false)
                changeTab(4)
            }
        }
    }
   fun resetResouce(){
       image1.setImageDrawable(getDrawable(tabItems[0].unSelectedIcon))
        tv1.setTextColor(resources.getColor(tabItems[0].getTitleUnSelectColor()))
       image2.setImageDrawable(getDrawable(tabItems[1].unSelectedIcon))
       tv2.setTextColor(resources.getColor(tabItems[0].getTitleUnSelectColor()))
       image3.setImageDrawable(getDrawable(tabItems[2].unSelectedIcon))
       tv3.setTextColor(resources.getColor(tabItems[0].getTitleUnSelectColor()))
       image4.setImageDrawable(getDrawable(tabItems[3].unSelectedIcon))
       tv4.setTextColor(resources.getColor(tabItems[0].getTitleUnSelectColor()))
       image5.setImageDrawable(getDrawable(tabItems[4].unSelectedIcon))
       tv5.setTextColor(resources.getColor(tabItems[0].getTitleUnSelectColor()))
    }

    override fun initData(){
       // super.initData()
        mPresenter!!.getNotice()
        mPresenter!!.updateVersion("1")
    }
    fun getdata(data: VersionBean){
        Log.e("versionbean",data.toString())
        if(data.versions.vername!=null){
            if(APKVersionCodeUtils.compareVersion(data.versions.vername,APKVersionCodeUtils.getVerName(App.INSTANCE))==1){

                var dialog1 = MaterialDialog.Builder(this)
                dialog = dialog1.customView(R.layout.activity_alert,false)
                    .autoDismiss(true)
                    .cancelable(true)
                    .show()
                var customView: View = dialog!!.getCustomView()!!
                (customView.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
                (customView.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
                (customView.parent.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
                (customView.parent.parent.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
                (customView.parent.parent.parent.parent.parent as ViewGroup).setBackgroundColor(
                    Color.TRANSPARENT
                )
                customView.findViewById<TextView>(R.id.tv_title).text = "版本升级"
                customView.findViewById<TextView>(R.id.tv_content).text = data.versions.content
                customView.findViewById<ImageView>(R.id.ivClose).setOnClickListener{
                    dialog!!.dismiss()
                }
                customView.findViewById<TextView>(R.id.tv_shengji).visibility = View.VISIBLE
                customView.findViewById<TextView>(R.id.tv_shengji).setOnClickListener{
                    ARouter.getInstance().build(ArouterAddress.WEBCTIVITY).withString("url",data.versions.url).navigation()
                }
        }

           /* val dialog =  Builder(this)
                .customView(R.layout.activity_alert,false)
                .autoDismiss(true)
                .cancelable(true)
                .show()
            val customView: View = dialog.getCustomView()!!
            (customView.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
            (customView.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
            (customView.parent.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
            (customView.parent.parent.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
            (customView.parent.parent.parent.parent.parent as ViewGroup).setBackgroundColor(
                Color.TRANSPARENT
            )
            customView.findViewById<TextView>(R.id.tv_title).text = "版本升级"
            customView.findViewById<TextView>(R.id.tv_content).text = data.versions.content
            customView.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
                dialog.dismiss()
            }
            customView.findViewById<TextView>(R.id.tv_shengji).visibility = View.VISIBLE
            customView.findViewById<TextView>(R.id.tv_shengji).setOnClickListener {
            ARouter.getInstance().build(ArouterAddress.WEBCTIVITY).withString("url",data.versions.url).navigation()
            }*/
        }

    }
    var dialog:MaterialDialog?=null
    fun getNotice(data: NoticeBean){
        SPUtil.put(SPConfig.ISFIRST,"2")
        var dialog1 =  MaterialDialog.Builder(this)

           dialog = dialog1.customView(R.layout.activity_alert,false)
               .autoDismiss(true)
               .cancelable(true)
               .show()
        var customView: View = dialog!!.getCustomView()!!
        (customView.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
        (customView.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
        (customView.parent.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
        (customView.parent.parent.parent.parent as ViewGroup).setBackgroundColor(Color.TRANSPARENT)
        (customView.parent.parent.parent.parent.parent as ViewGroup).setBackgroundColor(
            Color.TRANSPARENT
        )
        customView.findViewById<TextView>(R.id.tv_title).text = data.notice.biaoti
        RichText.from(data.notice.message).into(customView.findViewById<TextView>(R.id.tv_content))
        customView.findViewById<ImageView>(R.id.ivClose).setOnClickListener {
            dialog!!.dismiss()
        }

    }

    override fun onDestroy() {
       // dialog!!.dismiss()
        super.onDestroy()

    }

}
